package com.fatri.lessionsimple.opengl

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.util.Log
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class SimpleRender(clazz: Class<out IDrawer>, ctx: Context) : GLSurfaceView.Renderer {
    var mDrawer: IDrawer

    init {
        val constructor = clazz.getConstructor(Context::class.java)
        constructor.isAccessible = true
        mDrawer = constructor.newInstance(ctx)
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES20.glClearColor(0f, 0f, 0f, 0f)
        Log.e("CZB", "onSurfaceCreated:${Thread.currentThread().name}")
//        mDrawer.setTextureID(OpenGLTools.createTextureIds(1)[0])
        mDrawer.onSurfaceCreated()
    }


    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
        Log.e("CZB", "onSurfaceChanged:${Thread.currentThread().name}")
        mDrawer.onSurfaceChanged(width, height)
    }


    override fun onDrawFrame(gl: GL10?) {
        Log.e("CZB", "onDrawFrame:${Thread.currentThread().name}")
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
        mDrawer.onDrawFrame()
    }
}